﻿Ext.ns('suce.uit');

suce.uit.list = function (config) {

    return new Ext.Panel(Ext.apply({

        title: 'UIT',
        layout: 'border',

        initComponent: function () {
            var me = this,
                form,
                north;

            north = {
                region: 'north',
                height: 75,
                baseCls: 'x-plain',
                layout: {
                    type: 'hbox',
                    padding: '2',
                    align: 'stretch'
                },
                defaults: { headerPosition: 'bottom', border: false },
                items: [
                    {
                        xtype: 'buttongroup',
                        defaultType: '',
                        title: 'Acciones',
                        defaults: { scale: 'medium', iconAlign: 'top' },
                        items: [
                            BUTTON('Editar', me.edit, me, { iconCls: 'tb-edit24' }),
                            BUTTON('Actualizar', me.reload, me, { iconCls: 'tb-docexchange24' })
                        ]
                    }
                ]
            };

            me.grid = new Ext.grid.Panel({
                region: 'center',
                store: STORE('mant.ashx', { C: 'UIT', S: 'GETLIST' }, 'idparametro,codiparametro,nombparametro,valoparametro'),
                columns: COLMODEL([
                    ['nombparametro', 'Nombre', 200],
                    ['valoparametro', 'Valor', 0.1, {renderer : formatNumber, align:'right'}]
                ], { sortable: true })
            });

            me.items = [north, me.grid];
            Ext.Panel.prototype.initComponent.call(me);
        },

        reload: function () {
            var me = this;
            me.grid.store.load();
        },

        getForm: function () {
            var me = this;
            if (!me.dlgEdit) {
                me.dlgEdit = suce.uit.edit({ parent: me });
                me.dlgEdit.on({
                    scope: me,
                    'update': function (v) {
                        var r = GETSELECTED(me.grid);
                        r.set(v);
                        r.commit();
                    }
                });
            }
            return me.dlgEdit;
        },

        edit: function () {
            var me = this,
                r = GETSELECTED(me.grid);
            if (!r) return;
            me.getForm().show({ idparametro: r.data.idparametro });
        }

    }, config));
};

suce.uit.edit = function (config) {

    return new Ext.Dialog(Ext.apply({

        title: 'Edicion',
        height: 80,
        width: 200,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('insert', 'update');

            me.bbar = ['->',
                { text: '<b>Aceptar</b>', handler: me.ok, scope: me, iconCls: 'tb-save' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { labelWidth: 50 },
                items: [
                    HIDDEN('idparametro'),
                    NUMBER('valoparametro', 'Valor', { anchor: '-20', allowBlank:false })
                ]
            });

            me.items = [form];
            Ext.Dialog.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) { // p --> {idparametro}
            var me = this;
            p = p || {};
            Ext.Dialog.prototype.show.call(me);
            callServer('mant.ashx', { C: 'UIT', S: 'GETBYID', idparametro: p.idparametro }, function (v) {
                v = Ext.decode(v);
                me.form.setValues(v);
            }, me);
            
        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'UIT', S: 'UPDATE' }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('mant.ashx', p, function (v, j) {
                if (me.fireEvent('update', p) !== false)
                    me.onEsc();
            }, me);
        }

    }, config));
};